<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/12-15:08
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\controller;
    use app\wechat\service\WechatService;

    class Tools extends ChatBase
    {

        /**
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: oauth
         * @describe:网页授权测试
         */
        public function oauth()
        {
            $this->url = $this->request->url(true);
            $this->fans = WechatService::getWebOauthInfo($this->url, 1);
            return $this->fetch();
        }



        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: oauth_qrc
         * @describe:显示网页授权二维码
         */
        public function oauth_qrc()
        {
            $url = url('@wechat/tools/oauth', '', true, true);

            return $this->showQrc($url);
        }

        /**
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: jssdk
         * @describe:JSSDK测试
         */
        public function jssdk()
        {
            $this->options = WechatService::getWebJssdkSign();
            return $this->fetch();
        }

        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: jssdk_qrc
         * @describe:显示网页授权二维码
         */
        public function jssdk_qrc()
        {
            $this->url = url('@wechat/tools/jssdk', '', true, true);
            return $this->showQrc($this->url);
        }



        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: scanOneQrc
         * @describe:微信扫码支付模式一二维码显示
         */
        public function scanOneQrc()
        {
            $pay = WechatService::WePayOrder();
            $result = $pay->qrcParams('8888888');
            $this->showQrc($result);
        }


        /**
         * @return string
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: scanOneNotify
         * @describe: 微信扫码支付模式一通知处理 ,注意，需要在微信商户配置支付通知地址
         */
        public function scanOneNotify()
        {
            $pay = WechatService::WePayOrder();
            $notify = $pay->getNotify();
            p('======= 来自扫码支付1的数据 ======');
            p($notify);
            // 产品ID 你的业务代码，并实现下面的统一下单操作
            $product_id = $notify['product_id'];
            // 微信统一下单处理
            $options = [
                'body'             => '测试商品，产品ID：' . $product_id,
                'out_trade_no'     => time(),
                'total_fee'        => '1',
                'trade_type'       => 'NATIVE',
                'notify_url'       => url('@wechat/tools/notify', '', true, true),
                'spbill_create_ip' => request()->ip(),
            ];
            $order = $pay->create($options);
            p('======= 来自扫码支付1统一下单结果 ======');
            p($order);
            // 回复XML文本
            $result = [
                'return_code' => 'SUCCESS',
                'return_msg'  => '处理成功',
                'appid'       => $notify['appid'],
                'mch_id'      => $notify['mch_id'],
                'nonce_str'   => \WeChat\Contracts\Tools::createNoncestr(),
                'prepay_id'   => $order['prepay_id'],
                'result_code' => 'SUCCESS',
            ];
            $result['sign'] = $pay->getPaySign($result);
            p('======= 来自扫码支付1返回的结果 ======');
            p($result);
            return \WeChat\Contracts\Tools::arr2xml($result);
        }



        /**
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: scanQrc
         * @describe:扫码支付模式二测试二维码
         */
        public function scanQrc()
        {
            $pay = WechatService::WePayOrder();

            $result = $pay->create([
                'body'             => '测试商品',
                'out_trade_no'     => time(),
                'total_fee'        => '1',
                'trade_type'       => 'NATIVE',
                'notify_url'       => url('@wechat/tools/notify', '', true, true),
                'spbill_create_ip' => request()->ip(),
            ]);
            $this->showQrc($result['code_url']);
        }



        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: jsapiQrc
         * @describe:微信JSAPI支付二维码
         */
        public function jsapiQrc()
        {
            $this->url = url('@wechat/tools/jsapi', '', true, true);
            return $this->showQrc($this->url);
        }




        /**
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: jsapi
         * @describe:微信JSAPI支付测试
         */
        public function jsapi()
        {
            $pay = WechatService::WePayOrder();
            $openid = WechatService::getWebOauthInfo(request()->url(true), 0)['openid'];
            $options = [
                'body'             => '测试商品',
                'out_trade_no'     => time(),
                'total_fee'        => '1',
                'openid'           => $openid,
                'trade_type'       => 'JSAPI',
                'notify_url'       => url('@wechat/tools/notify', '', true, true),
                'spbill_create_ip' => request()->ip(),
            ];
            // 生成预支付码
            $result = $pay->create($options);
            // 创建JSAPI参数签名
            $options = $pay->jsapiParams($result['prepay_id']);
            $optionJSON = json_encode($options, JSON_UNESCAPED_UNICODE);
            // JSSDK 签名配置
            $configJSON = json_encode(WechatService::getWebJssdkSign(), JSON_UNESCAPED_UNICODE);

            echo '<pre>';
            echo "当前用户OPENID: {$openid}";
            echo "\n--- 创建预支付码 ---\n";
            var_export($result);
            echo '</pre>';

            echo '<pre>';
            echo "\n\n--- JSAPI 及 H5 参数 ---\n";
            var_export($options);
            echo '</pre>';
            echo "<button id='paytest' type='button'>JSAPI支付测试</button>";
            echo "
        <script src='//res.wx.qq.com/open/js/jweixin-1.2.0.js'></script>
        <script>
            wx.config($configJSON);
            document.getElementById('paytest').onclick = function(){
                var options = $optionJSON;
                options.success = function(){
                    alert('支付成功');
                }
                wx.chooseWXPay(options);
            }
        </script>";
        }



        /**
         * @return string
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: notify
         * @describe:支付通知接收处理
         */
        public function notify()
        {
            $wechat = WechatService::WePayOrder();
            p($wechat->getNotify());
            return 'SUCCESS';
        }



        /**
         * @param $url
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: showQrc
         * @describe:创建二维码响应对应
         */
        protected function showQrc($url)
        {
            $qrCode = new \Endroid\QrCode\QrCode();

            $qrCode->setText($url)->setSize(300)->setPadding(20)->setImageType('png');

            response($qrCode->get(), 200, ['Content-Type' => 'image/png'])->send();
        }


    }